package com.zym.utils.EasyExcel;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zym.constant.ExcelConstants;
import com.zym.entity.Emp;
import com.zym.service.EmpService;
import com.zym.vo.EmpVo;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class EasyExceGeneralDatalListener extends AnalysisEventListener<EmpVo> {
    /**
     * 处理业务逻辑的Service,也可以是Mapper
     */
    private EmpService empService;

    /**
     * 用于存储读取的数据
     */
    private List<EmpVo> dataList = new ArrayList<>();
    //导入成功的数量
    private int SuccessCount =0;
    private HttpServletResponse response;
    //错误数据
    private List<EmpVo> failList=new ArrayList<>();

    public List<EmpVo> getFailList() {
        return failList;
    }

    public void setFailList(List<EmpVo> failList) {
        this.failList = failList;
    }

    public EasyExceGeneralDatalListener(EmpService empService,HttpServletResponse response) {
        this.empService = empService;
        this.response=response;
    }

    @Override
    public void invoke(EmpVo empVo, AnalysisContext context) {
        String errorMsg = null;
        try {
            errorMsg = ExcelValidateHelper.validateEntity(empVo);

            System.out.println(errorMsg);
        }  catch (NoSuchFieldException e) {
            errorMsg = "解析数据出错";
            e.printStackTrace();
        }
        if (StringUtils.isBlank(errorMsg)){
            //数据add进入集合
            dataList.add(empVo);
        }else {
            empVo.setErrorMsg(errorMsg);
            failList.add(empVo);
        }

        //size是否为100000条:这里其实就是分批.当数据等于10w的时候执行一次插入
        if (dataList.size() >= ExcelConstants.GENERAL_ONCE_SAVE_TO_DB_ROWS) {
            //存入数据库:数据小于1w条使用Mybatis的批量插入即可;
            saveData();
            //清理集合便于GC回收
            dataList.clear();
        }
    }







    /**
     * 保存数据到DB
     *
     * @param
     * @MethodName: saveData
     * @return: void
     */
    private void saveData() {
        List<Emp> empList=new ArrayList<>();
        for (EmpVo empVo :dataList) {
            Emp emp = new Emp();
            BeanUtils.copyProperties(empVo,emp);
            empList.add(emp);


        }
        this.empService.saveBatch(empList);
        SuccessCount+=empList.size();
        dataList.clear();
    }

    /**
     * Excel中所有数据解析完毕会调用此方法
     *
     * @param: context
     * @MethodName: doAfterAllAnalysed
     * @return: void
     */

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        dataList.clear();
        System.out.println(StrUtil.format("一共成功导入{}数据",SuccessCount));
        System.out.println(StrUtil.format("一共有误{}数据",failList.size()));
        //自动导出错误的数据
        try {
            ExcelUtil.writeExcel(response,"错误数据",EmpVo.class,failList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        failList.clear();
        SuccessCount=0;
    }
}
